const express = require('express')
const app = express()
const router = express.Router()
const utils = require('utility')
const path = require('path')
const db = require(path.join(__dirname, '../common/db.js'))

路由配置
router.get('/users/:id', async (req, res) => {
    let param = req.params
    let sql = 'select * from user where id = ?'
    let info = await db.operateData(sql, [param.id])
    if (info && info.length > 0) {
        res.json({
            status: 0,
            message: '获取用户信息成功',
            data: info[0]
        })
    } else {
        res.json({
            status: 1,
            message: '获取用户信息失败'
        })
    }
})

根据id获取用户信息


// 获取用户信息
router.get('/users', async (req, res) => {
    let param = req.query
    if (!param.pagenum || !param.pagesize) {
        return res.json({
            status: 1,
            message: '参数不正确！'
        })
    }
    param.pagenum = parseInt(param.pagenum)
    param.pagesize = parseInt(param.pagesize)

    let sql = 'select id, username, nickname, email, user_pic from user where status = 2 limit ?, ?'
    let info = await db.operateData(sql, [param.pagesize * (param.pagenum - 1), param.pagesize])
    let csql = 'select count(*) as total from user where status = 2'
    let cret = await db.operateData(csql)
    if (info && info.length > 0) {
        res.json({
            status: 0,
            message: '获取用户信息成功！',
            data: info,
            total: cret[0].total
        })
    } else {
        res.json({
            status: 1,
            message: '获取用户信息失败！'
        })
    }
})

// 添加用户信息
router.post('/users', async (req, res) => {
    // 获取请求参数
    let param = req.body
    param.password = utils.md5(param.password)

    let checkSql = 'select id from user where username = ?'
    let flag = await db.operateData(checkSql, [param.username])
    console.log(flag)
    if (flag && flag.length > 0) {
        res.json({
            status: -1,
            message: '用户名已存在，请更换一个！'
        })
        return
    }

    // 更新用户的信息
    let sql = 'insert user set ?'
    // 如果是增删改操作，那么返回对象；如果是查询，那么返回数组
    let ret = await db.operateData(sql, [{ username: param.username, password: param.password, nickname: param.nickname, email: param.email, status: 2 }])
    // 处理响应状态
    if (ret && ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '添加用户信息成功！'
        })
    } else {
        res.json({
            status: 1,
            message: '添加用户信息失败！'
        })
    }
})

// 重置密码
router.put('/users/:id', async (req, res) => {
    let param = req.params
    let password = utils.md5(req.body.password)
    let sql = 'update user set password = ? where id = ?'
    let ret = await db.operateData(sql, [password, param.id])
    if (ret && ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '重置密码成功！'
        })
    } else {
        res.json({
            status: 1,
            message: '重置密码失败！'
        })
    }
})


// 更新用户信息
router.put('/upuser', async (req, res) => {
    // 获取请求参数
    let param = req.body
    // 更新用户的信息
    let sql = 'update user set ? where id = ?'
    // 如果是增删改操作，那么返回对象；如果是查询，那么返回数组
    let ret = await db.operateData(sql, [{ nickname: param.nickname, email: param.email }, param.id])
    // 处理响应状态
    if (ret && ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '修改用户信息成功！'
        })
    } else {
        res.json({
            status: 1,
            message: '修改用户信息失败！'
        })
    }
})